package com.ldk.base.framework.util.sms;

import java.net.URLEncoder;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.ldk.base.framework.util.HttpUtil;

/**
 * 北程短信接口
 * @author xiangwl
 *
 */
public class BechtechSms  implements ISmsApi {

	private static final String BC_SMS_URL="http://sms.bechtech.cn/Api/send/data/json";
//	private static final String BC_SMS_ACCESS_KEY="3215";
//	private static final String BC_SMS_SECRET_KEY="40dbc9ce1405365530b63a87eeff6c6ca379c3f9";


	@Override
	public Boolean send(SmsConfig config) {
		if(config == null) return false;
		if(StringUtils.isBlank(config.getAccessKey()) ||
		   StringUtils.isBlank(config.getAccessPwd()) ||
		   StringUtils.isBlank(config.getPhonNos()) ||
		   StringUtils.isBlank(config.getContent()))
			return false;
		String encodeContent = config.getContent();
		try{
			encodeContent = URLEncoder.encode(config.getContent(), "utf-8");
		}
		catch(Exception e){
			
		}

		String params=String.format("accesskey=%s&secretkey=%s&mobile=%s&content=%s",
									config.getAccessKey(),
									config.getAccessPwd(),
									config.getPhonNos(),
									encodeContent);
		
		String result = HttpUtil.get(BC_SMS_URL, params, null);
		System.out.println("BechtechSms::Response::"+result);
		if(StringUtils.isEmpty(result))
			return false;
		BechtechSmsResult bechtechSmsResult = JSON.parseObject(result, BechtechSmsResult.class);
		if(bechtechSmsResult != null && bechtechSmsResult.getResult().equalsIgnoreCase("01")){
			return true;
		}
		return false;
	}
	
	/**
	 * 北程短信响应对象
	 * @author xiangwl
	 *
	 */
	protected class BechtechSmsResult{
		private  String result;
		private  String msgid;
		private  String desc;
		public String getResult() {
			return result;
		}
		public String getMsgid() {
			return msgid;
		}
		public String getDesc() {
			return desc;
		}
		public void setResult(String result) {
			this.result = result;
		}
		public void setMsgid(String msgid) {
			this.msgid = msgid;
		}
		public void setDesc(String desc) {
			this.desc = desc;
		}
	}
	
}
